package com.hctforgreen.greenservice.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.hctforgreen.greenservice.R;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;


/**
 * 检查当前网络
 * @author 390694
 *
 */
public class CheckNetworkUtil {
	
	/**
	 * 检查网络
	 * @param activity
	 * @return
	 */
	public static boolean checkNetwork(Activity activity){
		ConnectivityManager connectivityManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo == null) {
			
			return false;
			
		}else{			
			return true;
//			if (isNetworkAvailable()) {
//				return true;
//			}else{
//				ToastUtil.showToast(activity, R.string.disaster_recovery_unavailable_network);
//				return false;
//			}
		}	
	}
	/**
	 * 检查当前网络是否可以上网
	 * @return
	 */
	public static boolean isNetworkAvailable(){
		
		String result = null;
		String ip = "www.baidu.com";//ping 的地址，
		try {
			
			Process process = Runtime.getRuntime().exec("ping -c 1 -w 1 "+ ip);//ping 百度
		    //读取ping的内容，可以不用
			InputStream inputStream = process.getInputStream();
			BufferedReader stringBufferedReader = new BufferedReader(new InputStreamReader(inputStream));
			StringBuffer stringBuffer = new StringBuffer();
			String content = "";
			while ((content = stringBufferedReader.readLine()) != null) {
				stringBuffer.append(content);			
			}
			LogUtil.i("DATA", " ping百度结果    " + stringBuffer.toString());
			
			//ping 结果
			int status = process.waitFor();
			LogUtil.i("DATA", " ping status    " + status);
			process.destroy();
			if (status == 0) {
				
				result = "success";
				
				return true;
			}else{
				
				result = "failed";
			}
			
		} catch (IOException e) {
			
			e.printStackTrace();
			result="IOException";
		} catch (InterruptedException e) {
			result="InterruptedException";
			e.printStackTrace();
		}finally{
			//Log.i("DATA", " ping百度    result===" + result);
		}
		return false;
	
		
	}

	

}
